package com.manager.core.ad.mbactivity

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.ViewGroup
import com.manager.core.master.scope.AdParams
import com.manager.core.master.view.AutoConfigAdViewScope
import com.manager.core.master.view.ScopeFinishListener
import com.manager.core.pl190.host668.handle.HandlerPostAdInterface
import java.lang.ref.WeakReference

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
open abstract class MBBaseFullscreenActivity : RouteActivity() {

    private val TAG = "MBBaseFullscreenActivit"

    lateinit var mAdIntercepterHandle: HandlerPostAdInterface

    var adkey: String? = ""

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate() called")
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        hide()
        adkey = intent.getStringExtra(HandlerPostAdInterface.HANDLE_ADKEY)
        val adIndex = intent.getIntExtra(HandlerPostAdInterface.HANDLE_ADINDEX, -1)

        if (TextUtils.isEmpty(adkey) || adIndex < 0) {
            Log.d(TAG, "onCreate: params is vail")
            finish()
            return
        }

        mAdIntercepterHandle = HandlerPostAdInterface.copy(adkey, adIndex)


        if (mAdIntercepterHandle == null) {
            finish()
        }
        Log.d(TAG, "onCreate: adkey = ${adkey} index = ${adIndex}")
    }


    fun toNext() {

        if (mAdIntercepterHandle != null) {
            if (!mAdIntercepterHandle.hasNext()) {
                finish()
                return
            }
        }

        val autoConfigAdViewScope = AutoConfigAdViewScope()
        val AdParams = AdParams().also {
            it.activity = WeakReference(this)
            it.adkey = adkey
            it.continaer = WeakReference(getFullContentView())
        }
        autoConfigAdViewScope.show(AdParams, mAdIntercepterHandle, object : ScopeFinishListener {
            override fun showLoading() {
//                MToast.show("开始加载")
            }

            override fun hideLoading() {
//                MToast.show("加载完成")
            }

            override fun onComplete() {
//                MToast.show("完成")
                mFinish()
            }

            override fun toNextAndFinish() {
//                MToast.show("场景转换")
                mFinish()
            }
        })
    }

    abstract fun getFullContentView(): ViewGroup?

    private fun hide() {
        // Hide UI first
        supportActionBar?.hide()
    }


    override fun mFinish() {
        if (isFinishing) {
            return
        }
        finish()
    }
}